package com.dididoctor.doctor.Activity.Main.Message;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Activity.PatientDetial.PatientDetialTHActivity;
import com.dididoctor.doctor.Activity.Usercentre.Authentication.AuthenticationActivity;
import com.dididoctor.doctor.Activity.Usercentre.MyPurse.MyPurseActivity;
import com.dididoctor.doctor.Activity.Welcome.WelcomeDialog;
import com.dididoctor.doctor.Activity.mvp.MainActivity;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.Http.MyHttpResponseHandler;
import com.dididoctor.doctor.MV.BasePresenter;
import com.dididoctor.doctor.MV.BusinessClient;
import com.dididoctor.doctor.MV.Response;
import com.dididoctor.doctor.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 2.0
 * @类描述： 登陆页
 * @项目名称：ddgh
 * @类名称： loginActivity
 * @包名称： com.doudoukeji.service.Activity.login
 * @创建人：张强 boy
 * @创建时间：2016/3/7 16:40
 * @修改人：
 * @修改时间：2016/3/7 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class MessagePresenter extends BasePresenter {
    private MessageView view;

    public MessagePresenter(Context context, MessageView view) {
        super(context, view);
        this.view = view;
    }

    public void getOrderList(int pageIndex) {
//        userId	String	是	用户
//        token	String	是
//        pageIndex	String	否	页码，第一页从开始
//        pageSize	String	否	页容量
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        params.put("pageSize", ConstantValue.LOAD_MESSAGE_COUNT);
        params.put("pageIndex", pageIndex);
        BusinessClient.post(ConstantValue.messageInfoUrl, params, new MyHttpResponseHandler(view) {
            private List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
            private List<MessageBean> orderBeans = new ArrayList<MessageBean>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    maps = response.getListData("messageList");
//                    "messageId":"1",//消息id
//                            "title":"",//标题
//                            "contents":"1",//消息内容
//                            "webUrl":"",//链接地址
                    for (Map<String, String> map : maps) {
                        MessageBean messageBean = new MessageBean();
                        String contents = Util.toString(map.get("contents"));
                        if (contents.indexOf("type") !=-1) {
                            JSONObject object = JSON.parseObject(contents);
                            String type = object.getString("type");
                            messageBean.setType(type);
                            if("2".equals(type)){
//                                    2.电话咨询过期/图文咨询过期剩余30分钟时
//                                    内容:您有一个关于电话咨询/图文咨询订单即将过期，快去看看吧
//                                    点击:订单详情界面
                                messageBean.setClientId(object.getString("clientId"));
                                messageBean.setStatus(object.getString("status"));
                                messageBean.setIsSign(object.getString("isSign"));
                                messageBean.setAccId(object.getString("accId"));
                                messageBean.setOrderType( object.getString("orderType"));
                            }else if("3".equals(type)){
//                                    3.认证通过/失败
                                messageBean.setIsPass(object.getString("isPass"));
                            }else if("7".equals(type)){
//                                5.当患者向医生发起咨询申请的时候
//                                    患者端向医生提交订单的时候，医生需要接受到通知用户XXX向您发送了图文咨询申请，电话预约申请，点击跳转到对应订单详情界面
                                messageBean.setClientId(object.getString("clientId"));
                                messageBean.setStatus(object.getString("status"));
                                messageBean.setIsSign(object.getString("isSign"));
                                messageBean.setAccId(object.getString("accId"));
                                messageBean.setOrderType(object.getString("orderType"));
                            }else if ("8".equals(type)){
//                                    6.医生已经对订单进行处理但是 当订单结束时尚未下达诊疗建议
//                                    用户XXX于2016-2-4 12:00向您发起的图文咨询/预约电话订单已经完结，您还没有给他下达诊断建议，只有下达诊断建议您才可以获得诊费 点击跳入该订单的订单详情界面
                                messageBean.setClientId(object.getString("clientId"));
                                messageBean.setStatus(object.getString("status"));
                                messageBean.setIsSign(object.getString("isSign"));
                                messageBean.setAccId(object.getString("accId"));
                                messageBean.setOrderType(object.getString("orderType"));

                            }else if ("9".equals(type)){
                                String des = object.getString("content").toString();
                                messageBean.setTitle(des);
                            }
                            String des = object.getString("msg");
                            messageBean.setContents(des);
                            messageBean.setStartTime(Util.toString(map.get("startTime")));
                        }else{
                            messageBean.setMessageId(Util.toString(map.get("messageId")));
                            messageBean.setTitle(Util.toString(map.get("title")));
                            messageBean.setContents(Util.toString(map.get("contents")));
                            messageBean.setWebUrl(Util.toString(map.get("webUrl")));
                            messageBean.setStartTime(Util.toString(map.get("startTime")));
                        }

                        orderBeans.add(messageBean);
                    }
                    view.getdoctorsucced(orderBeans);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getdoctorfail();
            }
        });
    }
}
